unit fColecao;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ExtCtrls, FMTBcd, DB, SqlExpr, DBCtrls, StdCtrls, DBClient,
  SimpleDS, Mask, Grids, DBGrids, ComCtrls, FnpNumericEdit, WideStrings,
  DBXInterbase, frmDVDFilmes, DBXFirebird, Buttons, Spin,
  Jpeg, ExtDlgs, Enter, IdHTTP, UrlMon, Provider, OleCtrls, SHDocVw, MSHTML,
  Activex, Menus, IniFiles, IdBaseComponent, IdComponent, IdTCPConnection,
  IdTCPClient, ClipBrd;

const
  SGoogle = 'http://www.google.com.br/search?hl=pt-BR&source=hp&q=sinopse+filme+%s';
  SGoogleImg = 'http://www.google.com.br/images?client=opera&rls=pt-BR&q=filmes+%s';
  STorrentUP = 'http://www.google.com/search?q=%s&sitesearch=tup-cine.blogspot.com';
  SInterFilmes = 'http://www.interfilmes.com/busca.html?siteurl=www.interfilmes.com/busca.html?%s&cx=partner-pub-0638497024609863:arqvdxaqpa0&cof=FORID:11&ie=ISO-8859-1&q=%s&sa=Buscar';
  SAdoroCinema = 'http://www.adorocinema.com/busca/?criteria=%s';
  SAdoroCinemaExato = 'http://www.adorocinema.com/filmes/%s';

type
  TRGBArray = array[Word] of TRGBTriple;
  pRGBArray = ^TRGBArray;

  TF_Colecao = class(TForm)
    SQLConnection: TSQLConnection;
    sdsDVD: TSimpleDataSet;
    sdsDVDID_DVD: TIntegerField;
    dsDVD: TDataSource;
    sdsFilmes: TSimpleDataSet;
    sdsFilmesID_FILME: TIntegerField;
    sdsFilmesID_DVD: TIntegerField;
    sdsFilmesTITULO_ORIGINAL: TStringField;
    sdsFilmesTITULO: TStringField;
    sdsFilmesANO: TIntegerField;
    sdsFilmesID_TIPO_DVD: TIntegerField;
    sdsFilmesID_TIPO_QUALIDADE: TIntegerField;
    sdsFilmesLEGENDADO: TSmallintField;
    sdsFilmesDUBLADO: TSmallintField;
    sdsFilmesDUAL_AUDIO: TSmallintField;
    sdsFilmesNACIONAL: TSmallintField;
    sdsFilmesCAPA: TBlobField;
    sdsFilmesSINOPSE: TMemoField;
    sdsFilmesDURACAO: TSmallintField;
    sdsFilmesMIDIAS: TSmallintField;
    sdsFilmesTAMANHO: TSmallintField;
    sdsFilmesGENERO: TStringField;
    dsFilmes: TDataSource;
    dsPesquisa: TDataSource;
    sdsPesquisa: TSimpleDataSet;
    pgGeral: TPageControl;
    tabColecao: TTabSheet;
    Panel1: TPanel;
    tabPesquisa: TTabSheet;
    sdsFilme: TSimpleDataSet;
    sdsFilmeCAPA: TBlobField;
    sdsFilmeSINOPSE: TMemoField;
    sdsTipoDVD: TSimpleDataSet;
    sdsTipoDVDID_TIPO_DVD: TIntegerField;
    sdsTipoDVDDESCRICAO: TStringField;
    sdsTipoQualidade: TSimpleDataSet;
    sdsTipoQualidadeID_TIPO_QUALIDADE: TIntegerField;
    sdsTipoQualidadeDESCRICAO: TStringField;
    sdsTipoQualidadeOBS: TMemoField;
    dsTipoDVD: TDataSource;
    dsTipoQualidade: TDataSource;
    Label9: TLabel;
    Label10: TLabel;
    Label11: TLabel;
    Label12: TLabel;
    Bevel2: TBevel;
    lbTipo2: TLabel;
    Label14: TLabel;
    Label15: TLabel;
    Label16: TLabel;
    Label17: TLabel;
    Label18: TLabel;
    Label19: TLabel;
    Label20: TLabel;
    lbTitOriginal: TLabel;
    lbTitBrasil: TLabel;
    lbGenero: TLabel;
    lbAno: TLabel;
    lbDuracao: TLabel;
    lbDVD: TLabel;
    Label21: TLabel;
    Label13: TLabel;
    lbFilme: TLabel;
    edFilme: TEdit;
    ckLegendado: TCheckBox;
    ckDublado: TCheckBox;
    ckNacional: TCheckBox;
    DBGrid2: TDBGrid;
    edAno: TFnpNumericEdit;
    ckDual: TCheckBox;
    edSinopse: TMemo;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    Label5: TLabel;
    Label6: TLabel;
    Label7: TLabel;
    DBEdit3: TDBText;
    DBEdit4: TDBText;
    DBEdit6: TDBText;
    DBEdit11: TDBText;
    DBEdit5: TDBText;
    Label8: TLabel;
    lbTipo: TLabel;
    Bevel1: TBevel;
    DVDFilmes: TFRM_DVDFilmes;
    DBMemo1: TDBMemo;
    DBNavigator1: TDBNavigator;
    BitBtn1: TBitBtn;
    Label23: TLabel;
    edDVD: TSpinEdit;
    DBText1: TDBText;
    Label1: TLabel;
    DBText2: TDBText;
    Label22: TLabel;
    imgFilme: TImage;
    BitBtn3: TBitBtn;
    Label46: TLabel;
    Label47: TLabel;
    Label48: TLabel;
    Label49: TLabel;
    MREnter: TMREnter;
    PopupMenu2: TPopupMenu;
    Coleo2: TMenuItem;
    Cadastro2: TMenuItem;
    qFilmes: TSQLQuery;
    qSCapa: TSQLQuery;
    qSCapaTOTAL: TIntegerField;
    Image1: TImage;
    sdsFilmesVISTO: TSmallintField;
    BitBtn7: TBitBtn;
    sdsFilmesNOTA: TSmallintField;
    imgEstrela1: TImage;
    imgEstrela2: TImage;
    imgVisto1: TImage;
    imgVisto2: TImage;
    edGenero: TEdit;
    procedure DBGrid2DblClick(Sender: TObject);
    procedure sdsDVDAfterScroll(DataSet: TDataSet);
    procedure Button1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);

    procedure ChangePesquisa(Sender: TObject);
    procedure sdsFilmesAfterScroll(DataSet: TDataSet);
    procedure sdsPesquisaAfterScroll(DataSet: TDataSet);
    procedure edDVDKeyUp(Sender: TObject; var Key: Word; Shift: TShiftState);
    procedure BitBtn1Click(Sender: TObject);
    procedure sdsFilmesCadBeforeEdit(DataSet: TDataSet);
    procedure FormDblClick(Sender: TObject);
    procedure edCadTituloOrigEnter(Sender: TObject);
    procedure BitBtn7Click(Sender: TObject);
  private
    TFilmeTransaction: TTransactionDesc;
    FImagem: Boolean;
    FDVD,
      FCadCampo: integer;
    FPaginas: array [0..2000] of integer;
    procedure imgClick(Sender: TObject);

    procedure SelecionPagina(Sender: TObject);
  public
  end;

var
  F_Colecao: TF_Colecao;

implementation

uses fMainFilms;

{$R *.dfm}

procedure SmoothResize(Src, Dst: TBitmap);
var
  x, y: Integer;
  xP, yP: Integer;
  xP2, yP2: Integer;
  SrcLine1, SrcLine2: pRGBArray;
  t3: Integer;
  z, z2, iz2: Integer;
  DstLine: pRGBArray;
  DstGap: Integer;
  w1, w2, w3, w4: Integer;
begin
  Src.PixelFormat := pf24Bit;
  Dst.PixelFormat := pf24Bit;

  if (Src.Width = Dst.Width) and (Src.Height = Dst.Height) then
    Dst.Assign(Src)
  else
  begin
    DstLine := Dst.ScanLine[0];
    DstGap  := Integer(Dst.ScanLine[1]) - Integer(DstLine);

    xP2 := MulDiv(pred(Src.Width), $10000, Dst.Width);
    yP2 := MulDiv(pred(Src.Height), $10000, Dst.Height);
    yP  := 0;

    for y := 0 to pred(Dst.Height) do
    begin
      xP := 0;

      SrcLine1 := Src.ScanLine[yP shr 16];

      if (yP shr 16 < pred(Src.Height)) then
        SrcLine2 := Src.ScanLine[succ(yP shr 16)]
      else
        SrcLine2 := Src.ScanLine[yP shr 16];

      z2  := succ(yP and $FFFF);
      iz2 := succ((not yp) and $FFFF);
      for x := 0 to pred(Dst.Width) do
      begin
        t3 := xP shr 16;
        z  := xP and $FFFF;
        w2 := MulDiv(z, iz2, $10000);
        w1 := iz2 - w2;
        w4 := MulDiv(z, z2, $10000);
        w3 := z2 - w4;
        DstLine[x].rgbtRed := (SrcLine1[t3].rgbtRed * w1 +
          SrcLine1[t3 + 1].rgbtRed * w2 +
          SrcLine2[t3].rgbtRed * w3 + SrcLine2[t3 + 1].rgbtRed * w4) shr 16;
        DstLine[x].rgbtGreen :=
          (SrcLine1[t3].rgbtGreen * w1 + SrcLine1[t3 + 1].rgbtGreen * w2 +

          SrcLine2[t3].rgbtGreen * w3 + SrcLine2[t3 + 1].rgbtGreen * w4) shr 16;
        DstLine[x].rgbtBlue := (SrcLine1[t3].rgbtBlue * w1 +
          SrcLine1[t3 + 1].rgbtBlue * w2 +
          SrcLine2[t3].rgbtBlue * w3 +
          SrcLine2[t3 + 1].rgbtBlue * w4) shr 16;
        Inc(xP, xP2);
      end; {for}
      Inc(yP, yP2);
      DstLine := pRGBArray(Integer(DstLine) + DstGap);
    end; {for}
  end; {if}
end; {SmoothResize}

function LoadJPEGPictureStream(Bitmap: TBitmap; PStream: TMemoryStream): Boolean;
var
  JPEGImage: TJPEGImage;
begin
  if (PStream.Size = 0) then    // No Stream Size
    Result := False  //to load - return False...
  else
  begin
    try  // Start of try except
      JPEGImage := TJPEGImage.Create;  // Create the JPEG image... try  // now
      try  // to load the file but
        JPEGImage.LoadFromStream(PStream);
        // might fail...with an Exception.
        Bitmap.Assign(JPEGImage);
        // Assign the image to our bitmap.Result := True;
        // Got it so return True.
        Result := True;
      finally
        JPEGImage.Free;  // ...must get rid of the JPEG image. finally
      end; {try}
    except on E: Exception do
      begin
        Result := False; // Oops...never Loaded, so return False.
        ShowMessage(E.Message);
      end;
    end; {try}
  end; {if}
end; {LoadJPEGPictureFile}

function LoadJPEGPictureFile(Bitmap: TBitmap; FilePath, FileName: string): Boolean;
var
  JPEGImage: TJPEGImage;
begin
  if (FileName = '') then    // No FileName so nothing
    Result := False  //to load - return False...
  else
  begin
    try  // Start of try except
      JPEGImage := TJPEGImage.Create;  // Create the JPEG image... try  // now
      try  // to load the file but
        JPEGImage.LoadFromFile(FilePath + FileName);
        // might fail...with an Exception.
        Bitmap.Assign(JPEGImage);
        // Assign the image to our bitmap.Result := True;
        // Got it so return True.
      finally
        JPEGImage.Free;  // ...must get rid of the JPEG image. finally
      end; {try}
    except
      Result := False; // Oops...never Loaded, so return False.
    end; {try}
  end; {if}
end; {LoadJPEGPictureFile}

function SaveJPEGPictureStream(Bitmap: TBitmap; PStream: TMemoryStream;
  Quality: Integer): Boolean;
begin
  Result := True;
  try
    with TJPegImage.Create do
    begin
      try
        Assign(Bitmap);
        CompressionQuality := Quality;
        SaveToStream(PStream);
      finally
        Free;
      end; {try}
    end; {with}
  except
    raise;
    Result := False;
  end; {try}
end; {SaveJPEGPictureFile}

function SaveJPEGPictureFile(Bitmap: TBitmap; FilePath, FileName: string;
  Quality: Integer): Boolean;
begin
  Result := True;
  try
    if ForceDirectories(FilePath) then
    begin
      with TJPegImage.Create do
      begin
        try
          Assign(Bitmap);
          CompressionQuality := Quality;
          SaveToFile(FilePath + FileName);
        finally
          Free;
        end; {try}
      end; {with}
    end; {if}
  except
    raise;
    Result := False;
  end; {try}
end; {SaveJPEGPictureFile}

procedure ResizeImageBitmap(OldBitmap: TBitmap; NewBitmap: TBitmap; MaxWidth: Integer);
var
  aWidth: Integer;
begin
  aWidth := OldBitmap.Width;
  if (OldBitmap.Width > MaxWidth) then
  begin
    aWidth    := MaxWidth;
    NewBitmap.Width  := MaxWidth;
    NewBitmap.Height := MulDiv(MaxWidth, OldBitmap.Height, OldBitmap.Width);
    SmoothResize(OldBitmap, NewBitmap);
  end
  else
    NewBitmap.Assign(OldBitmap); {if}
end;

procedure ResizeImage(PStream: TMemoryStream; MaxWidth: Integer);
var
  OldBitmap: TBitmap;
  NewBitmap: TBitmap;
  aWidth: Integer;
begin
  OldBitmap := TBitmap.Create;
  try
    if LoadJPEGPictureStream(OldBitmap, PStream) then
    begin
      aWidth := OldBitmap.Width;
      if (OldBitmap.Width > MaxWidth) then
      begin
        aWidth    := MaxWidth;
        NewBitmap := TBitmap.Create;
        try
          NewBitmap.Width  := MaxWidth;
          NewBitmap.Height := MulDiv(MaxWidth, OldBitmap.Height, OldBitmap.Width);
          SmoothResize(OldBitmap, NewBitmap);
          SaveJPEGPictureStream(NewBitmap, PStream, 75);
        finally
          NewBitmap.Free;
        end; {try}
      end; {if}
    end; {if}
  finally
    OldBitmap.Free;
  end; {try}
end;

function JPEGDimensions(Filename : string; var X, Y : Word) : boolean;
var
  SegmentPos : Integer;
  SOIcount : Integer;
  b : byte;
begin
  Result  := False;
  with TFileStream.Create(Filename, fmOpenRead or fmShareDenyNone) do
  begin
    try
      Position := 0;
      Read(X, 2);
      if (X <> $D8FF) then
        exit;
      SOIcount  := 0;
      Position  := 0;
      while (Position + 7 < Size) do
      begin
        Read(b, 1);
        if (b = $FF) then begin
          Read(b, 1);
          if (b = $D8) then
            inc(SOIcount);
          if (b = $DA) then
            break;
        end; {if}
      end; {while}
      if (b <> $DA) then
        exit;
      SegmentPos  := -1;
      Position    := 0;
      while (Position + 7 < Size) do
      begin
        Read(b, 1);
        if (b = $FF) then
        begin
          Read(b, 1);
          if (b in [$C0, $C1, $C2]) then
          begin
            SegmentPos  := Position;
            dec(SOIcount);
            if (SOIcount = 0) then
              break;
          end; {if}
        end; {if}
      end; {while}
      if (SegmentPos = -1) then
        exit;
      if (Position + 7 > Size) then
        exit;
      Position := SegmentPos + 3;
      Read(Y, 2);
      Read(X, 2);
      X := Swap(X);
      Y := Swap(Y);
      Result  := true;
    finally
      Free;
    end; {try}
  end; {with}
end; {JPEGDimensions}

function GravaJPEG(PBitmap: TBitmap; PStream: TMemoryStream): Boolean;
var
  JPG: TJpegImage;
begin
  JPG := TJpegImage.Create;
  try
    JPG.CompressionQuality := 60;
    JPG.Assign(PBitmap);
    JPG.JPEGNeeded;
    JPG.PixelFormat := jf8bit;
    JPG.DibNeeded;
    JPG.SaveToStream(PStream);
  finally
    JPG.Free;
  end;
end;

function InverteStr(cStr: string): string;
var
  I: integer;
begin
  Result := '';
  for I := Length(cStr) downto 1 do
    Result := Result + cStr[I];
end;

function DownloadFile(SourceFile, DestinationFile: string): Boolean;
begin
  try
    Result := UrlDownloadToFile(nil, PChar(SourceFile), PChar(DestinationFile), 0, nil) = 0;
  except
    Result := False;
  end;
end;

procedure ConverterJPegParaBmp(Arquivo: string);
var
  JPeg: TJPegImage;
  Bmp: TBitmap;
begin
  JPeg := TJPegImage.Create;
  try
    JPeg.LoadFromFile(Arquivo);
    Bmp := TBitmap.Create;
    try
      Bmp.Assign(JPeg);
      Bmp.SaveToFile(ChangeFileExt(Arquivo, '.bmp'));

      DeleteFile(PChar(Arquivo));
    finally
      Bmp.Free;
    end;
  finally
    JPeg.Free;
  end;
end;

procedure TF_Colecao.BitBtn1Click(Sender: TObject);
begin
  pgGeral.ActivePage := tabPesquisa;
end;

procedure TF_Colecao.BitBtn7Click(Sender: TObject);
var
  cSTream: TMemoryStream;
begin
  qFilmes.Close;
  qFilmes.SQL.Clear;
  qFilmes.SQL.Add('select capa from filmes');
  qFilmes.Open;
  caption := '0';
  while not qFilmes.Eof do
  begin
    cSTream:=TMemoryStream.Create;
    try
    TBlobField(qFilmes.FieldByName('CAPA')).SaveToStream(cSTream);
    caption := IntToStr(StrToInt(caption)+(cSTream.Size div 1024));
    qFilmes.Next;
    finally
      cStream.Free;
    end;
    Application.ProcessMessages;
  end;
  caption := 'TOTAL: '+caption;

end;

procedure TF_Colecao.Button1Click(Sender: TObject);
begin
  with TF_MainFilms.Create(Application) do
  try
    ShowModal;
  finally
    Free;
  end;
end;

procedure TF_Colecao.ChangePesquisa(Sender: TObject);
var
  cSql: TStrings;
begin
  if (Trim(edFilme.Text) <> '') or
     (edGenero.Text <> '') or
     (edAno.Value > 1900) or
     ckLegendado.Checked or
     ckDublado.Checked or
     ckNacional.Checked or
     ckDual.Checked then
  with sdsPesquisa do
  try
    Close;
    cSql := TStringList.Create;
    cSql.Add('select');
    cSql.Add('  F.ID_FILME,');
    cSql.Add('  F.ID_DVD,');
    cSql.Add('  F.TITULO,');
    cSql.Add('  F.TITULO_ORIGINAL,');
    cSql.Add('  F.GENERO,');
    cSql.Add('  F.ANO,');
    cSql.Add('  F.DURACAO,');
    cSql.Add('  F.NACIONAL,');
    cSql.Add('  F.DUBLADO,');
    cSql.Add('  F.LEGENDADO,');
    cSql.Add('  F.DUAL_AUDIO,');
    cSql.Add('  F.VISTO,');
    cSql.Add('  F.NOTA');
    cSql.Add('from');
    cSql.Add('  FILMES F');
    cSql.Add('where');
    cSql.Add('  (1=1)');
    if Trim(edFilme.Text) <> '' then
    begin
      cSql.Add('and (upper(F.TITULO_ORIGINAL) like ''%''||upper('''+edFilme.Text+''')||''%'' or');
      cSql.Add('     upper(F.TITULO) like ''%''||upper('''+edFilme.Text+''')||''%'')');
    end;

    if edGenero.Text <> '' then
      cSql.Add('and F.GENERO = '+edGenero.Text);

    if edAno.Value > 1900 then
      cSql.Add('and F.ANO = '+FloatToStr(edAno.Value));

    if ckLegendado.Checked then
      cSql.Add('and F.LEGENDADO = 1');

    if ckDublado.Checked then
      cSql.Add('and F.DUBLADO = 1');

    if ckNacional.Checked then
      cSql.Add('and F.NACIONAL = 1');

    if ckDual.Checked then
      cSql.Add('and F.DUAL_AUDIO = 1');

    DataSet.CommandText := cSql.Text;

    //inputbox('','',DataSet.CommandText);
    Open;
  finally
    cSql.Free;
  end
  else
    sdsPesquisa.Close;
end;

procedure TF_Colecao.edCadTituloOrigEnter(Sender: TObject);
begin
  Clipboard.AsText := '';
  FCadCampo := TControl(Sender).Tag;
end;

procedure TF_Colecao.DBGrid2DblClick(Sender: TObject);
var
  I: integer;
begin
  if not sdsPesquisa.IsEmpty then
  begin
    sdsDVD.Locate('ID_DVD', sdsPesquisa.FieldByName('ID_DVD').AsInteger, []);
    for I := 0 to 20 do
      if FPaginas[I] = sdsPesquisa.FieldByName('ID_FILME').AsInteger then
      begin
        DVDFilmes.Position(I);
        Break;
      end;
    pgGeral.ActivePage := tabColecao;
  end;
end;

procedure TF_Colecao.edDVDKeyUp(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  if Key = VK_RETURN then
    sdsDVD.Locate('ID_DVD', edDVD.Value, []);
end;

procedure TF_Colecao.FormCreate(Sender: TObject);
var
  cDatabase: string;
  iniFile: TIniFile;
begin
  FCadCampo := 0;
  Clipboard.AsText := '';

  tabColecao.TabVisible := False;
  tabPesquisa.TabVisible := False;
  pgGeral.ActivePage := tabColecao;

  with SQLConnection.Params do
  begin
    SQLConnection.Close;
    iniFile := TIniFile.Create('.\4filmes.ini');
    try
      cDatabase := iniFile.ReadString('4filmes', 'database', '');
    finally
      iniFile.Free;
    end;
    Clear;
    Add('DriverName=Firebird');
    Add('Database='+cDatabase);
    Add('RoleName=RoleName');
    Add('User_Name=sysdba');
    Add('Password=masterkey');
    Add('ServerCharSet=');
    Add('SQLDialect=3');
    Add('ErrorResourceFile=');
    Add('LocaleCode=0000');
    Add('BlobSize=-1');
    Add('CommitRetain=False');
    Add('WaitOnLocks=True');
    Add('IsolationLevel=ReadCommitted');
    Add('Trim Char=False');
    SQLConnection.Open;
  end;

  sdsDVD.Open;
  sdsDVD.AfterScroll := nil;
  try
    edDVD.MinValue := sdsDVDID_DVD.AsInteger;
    sdsDVD.Last;
    edDVD.MaxValue := sdsDVDID_DVD.AsInteger;
    sdsDVD.First;
  finally
    sdsDVD.AfterScroll := sdsDVDAfterScroll;
  end;
  DVDFilmes.OnSelecionaPagina := SelecionPagina;

  sdsTipoDVD.Open;
  sdsTipoQualidade.Open;
  sdsFilmes.Open;
end;

procedure TF_Colecao.FormDblClick(Sender: TObject);
begin
  TFilmeTransaction.TransactionID := 1;
  TFilmeTransaction.IsolationLevel := xilREADCOMMITTED;
end;

procedure TF_Colecao.imgClick(Sender: TObject);
begin
end;

procedure TF_Colecao.sdsDVDAfterScroll(DataSet: TDataSet);
var
  I,
    nX,
    nID: integer;
  wS: TScrollBox;
  cBitmap: TBitmap;
  cStream: TMemoryStream;
  cFilmPage: TPage3d;
begin
{  for nL := Pred(scrollFilmes.ComponentCount) downto 0 do
    scrollFilmes.Components[nL].Free;
}
  nID := 0;
  with sdsFilmes do
  begin
    Close;
    Params.ParamByName('ID_DVD').AsInteger := sdsDVDID_DVD.AsInteger;
    Open;
    First;
    DVDFilmes.Clear;
    DVDFilmes.First;
    while not Eof do
    begin
      FPaginas[nID] := sdsFilmesID_FILME.AsInteger;
      Inc(nID);
      cStream := TMemoryStream.Create;
      try
        sdsFilmesCAPA.SaveToStream(cStream);
        cStream.Position := 0;

        cBitmap := TBitmap.Create;
        try
          LoadJPEGPictureStream(cBitmap, cStream);

          with cBitmap.Canvas do
          begin
            if cStream.Size = 0 then
            begin
              cBitmap.Width := 378;
              cBitmap.Height := 562;

              Brush.Style := bsSolid;
              Brush.Color := RGB(250, 249, 249);
              Pen.Style := psSolid;
              Pen.Color := RGB(250, 249, 249);
              Rectangle(0,0,216,244);

              Font.Name := 'Tahoma';
              Font.Size := 8;
              Font.Color := clRed;
              Font.Style := [fsBold];
              if sdsFilmesTITULO_ORIGINAL.AsString <> '' then
                TextOut(10,10, ' '+sdsFilmesTITULO_ORIGINAL.AsString+' ')
              else
                TextOut(10,10, ' '+sdsFilmesTITULO.AsString+' ');

              Font.Name := 'Tahoma';
              Font.Size := 16;
              Font.Color := clBlack;
              Font.Style := [fsBold];
              TextOut(80,90,' Sem ');
              TextOut(62,130,' Imagem ');
            end;

            if Boolean(sdsFilmesVISTO.AsInteger) then
              Draw(cBitmap.Width - 26,2,imgVisto1.Picture.Icon)
            else
              Draw(cBitmap.Width - 26,2,imgVisto2.Picture.Icon);

            nX := 2;
            for I := 1 to 5 do
            begin
              if sdsFilmesNOTA.AsInteger >= I then
                Draw(nX, 2, imgEstrela1.Picture.Icon)
              else
                Draw(nX, 2, imgEstrela2.Picture.Icon);
              Inc(nX, 26);
            end;
          end;

          cFilmPage:= DVDFilmes.AddBlankPage(cBitmap.Width, cBitmap.Height);
          cFilmPage.UpdateTexture(cBitmap);
        finally
          cBitmap.Free;
        end;
      finally
        cStream.Free;
      end;
      {wS := TScrollBox.Create(scrollFilmes);
      with wS do
      begin
        Parent := scrollFilmes;
        Width := 216;
        Height := 244;
        BorderStyle := bsNone;
        BevelKind := bkFlat;
        Tag := sdsFilmesID_FILME.AsInteger;
        Top := 0;
        Left := nL;
        //Color := nL;

        cStream := TMemoryStream.Create;
        try
          sdsFilmesCAPA.SaveToStream(cStream);
          cStream.Position := 0;

          wI := TImage.Create(wS);
          wI.Parent := wS;
          wI.Align := alClient;
          wI.Stretch := True;
          wI.Tag := sdsFilmesID_FILME.AsInteger;
          wI.Cursor := crHandPoint;
          wI.OnClick := imgClick;
          wI.Picture.Bitmap.LoadFromStream(cStream);

          if cStream.Size = 0 then
          with wI.Picture.Bitmap.Canvas do
          begin
            wI.Picture.Bitmap.Width := 216;
            wI.Picture.Bitmap.Height := 244;

            Brush.Style := bsSolid;
            Brush.Color := RGB(250, 249, 249);
            Pen.Style := psSolid;
            Pen.Color := RGB(250, 249, 249);
            Rectangle(0,0,216,244);

            Font.Name := 'Tahoma';
            Font.Size := 8;
            Font.Color := clRed;
            Font.Style := [fsBold];
            if sdsFilmesTITULO_ORIGINAL.AsString <> '' then
              TextOut(10,10, ' '+sdsFilmesTITULO_ORIGINAL.AsString+' ')
            else
              TextOut(10,10, ' '+sdsFilmesTITULO.AsString+' ');

            Font.Name := 'Tahoma';
            Font.Size := 16;
            Font.Color := clBlack;
            Font.Style := [fsBold];
            TextOut(80,90,' Sem ');
            TextOut(62,130,' Imagem ');
          end;
        finally
          cStream.Free;
        end;
      end;

      Inc(nL, 218);}
      Next;
    end;
    case nID of
      1, 2: nID := 0;
      3: nID := 1;
      else
        nID := 2;
    end;
    DVDFilmes.Position(nID);
    sdsFilmes.Locate('ID_FILME', FPaginas[nID], []);
  end;
end;

procedure TF_Colecao.sdsFilmesAfterScroll(DataSet: TDataSet);
var
  cStr: string;
begin
  cStr := 'Filme ';
  if sdsFilmesNACIONAL.AsInteger = 1 then
    cStr := cStr + 'Nacional, ';
  if sdsFilmesDUBLADO.AsInteger = 1 then
    cStr := cStr + 'Dublado, ';
  if sdsFilmesLEGENDADO.AsInteger = 1 then
    cStr := cStr + 'Legendado, ';
  if sdsFilmesDUAL_AUDIO.AsInteger = 1 then
    cStr := cStr + 'Dual Audio, ';

  cStr := Copy(cStr, 1, Length(cStr) -2);

  lbTipo.Caption := cStr;
end;

procedure TF_Colecao.sdsFilmesCadBeforeEdit(DataSet: TDataSet);
begin
  SqlConnection.StartTransaction(TFilmeTransaction);
end;

procedure TF_Colecao.sdsPesquisaAfterScroll(DataSet: TDataSet);
var
  I,
    nX: integer;
  cStr: string;
  cStream: TMemoryStream;
  cBitmap: TBitmap;
begin
  lbDVD.Caption := FormatFloat('00000', sdsPesquisa.FieldByName('ID_DVD').AsInteger);
  lbFilme.Caption := FormatFloat('000000', sdsPesquisa.FieldByName('ID_FILME').AsInteger);
  lbTitOriginal.Caption := sdsPesquisa.FieldByName('TITULO_ORIGINAL').AsString;
  lbTitBrasil.Caption := sdsPesquisa.FieldByName('TITULO').AsString;
  lbGenero.Caption := sdsPesquisa.FieldByName('GENERO').AsString;
  lbAno.Caption := sdsPesquisa.FieldByName('ANO').AsString;
  lbDuracao.Caption := sdsPesquisa.FieldByName('DURACAO').AsString;
  cStr := 'Filme ';
  if sdsPesquisa.FieldByName('NACIONAL').AsInteger = 1 then
    cStr := cStr + 'Nacional, ';
  if sdsPesquisa.FieldByName('DUBLADO').AsInteger = 1 then
    cStr := cStr + 'Dublado, ';
  if sdsPesquisa.FieldByName('LEGENDADO').AsInteger = 1 then
    cStr := cStr + 'Legendado, ';
  if sdsPesquisa.FieldByName('DUAL_AUDIO').AsInteger = 1 then
    cStr := cStr + 'Dual Audio, ';

  cStr := Copy(cStr, 1, Length(cStr) -2);

  lbTipo2.Caption := cStr;
  with sdsFilme do
  try
    Close;
    Params.ParamByName('ID_FILME').AsInteger := sdsPesquisa.FieldByName('ID_FILME').AsInteger;
    Open;

    edSinopse.Lines.Text := sdsFilmeSINOPSE.AsString;
    cStream := TMemoryStream.Create;
    try
      sdsFilmeCAPA.SaveToStream(cStream);
      cStream.Position := 0;

      cBitmap := TBitmap.Create;
      try
        LoadJPEGPictureStream(cBitmap, cStream);

        with cBitmap.Canvas do
          begin
          if cStream.Size = 0 then
          begin
            cBitmap.Width := 378;
            cBitmap.Height := 562;

            Brush.Style := bsSolid;
            Brush.Color := RGB(250, 249, 249);
            Pen.Style := psSolid;
            Pen.Color := RGB(250, 249, 249);
            Rectangle(0,0,216,244);

            Font.Name := 'Tahoma';
            Font.Size := 8;
            Font.Color := clRed;
            Font.Style := [fsBold];
            if sdsPesquisa.FieldByName('TITULO_ORIGINAL').AsString <> '' then
              TextOut(10,10, ' '+sdsPesquisa.FieldByName('TITULO_ORIGINAL').AsString+' ')
            else
              TextOut(10,10, ' '+sdsPesquisa.FieldByName('TITULO').AsString+' ');

            Font.Name := 'Tahoma';
            Font.Size := 16;
            Font.Color := clBlack;
            Font.Style := [fsBold];
            TextOut(80,90,' Sem ');
            TextOut(62,130,' Imagem ');
          end;

          if Boolean(sdsPesquisa.FieldByName('VISTO').AsInteger) then
            Draw(cBitmap.Width - 26,2,imgVisto1.Picture.Icon)
          else
            Draw(cBitmap.Width - 26,2,imgVisto2.Picture.Icon);

          nX := 2;
          for I := 1 to 5 do
          begin
            if sdsPesquisa.FieldByName('NOTA').AsInteger >= I then
              Draw(nX, 2, imgEstrela1.Picture.Icon)
            else
              Draw(nX, 2, imgEstrela2.Picture.Icon);
            Inc(nX, 26);
          end;
        end;

        imgFilme.Picture.Bitmap := cBitmap;
      finally
        cBitmap.Free;
      end;
    finally
      cStream.Free;
    end;
  finally
    Close;
  end;
end;

procedure TF_Colecao.SelecionPagina(Sender: TObject);
begin
  sdsFilmes.Locate('ID_FILME', FPaginas[DVDFilmes.PaginaSelecionada], []);
end;

initialization
  OleInitialize(nil);

finalization
  OleUninitialize;

end.
